package com.heima.leetcode.practice;

/**
 * @author 勾新杰
 * @version 1.0
 * @description: leetcode 82. 删除排序链表中的重复元素 II
 * @date 2025/5/16 10:39
 */
public class E82 {

    /**
     * 删除排序链表中的重复元素 II
     * @param head 头节点
     * @return 删除重复元素后的链表
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode sentinel = new ListNode(-101);
        sentinel.next = head;
        ListNode curr = head, last = sentinel, prev = sentinel;
        while (curr != null) {
            if (curr.next != null && curr.val == curr.next.val) {
                prev = curr;
                curr = curr.next;
            } else {
                if (curr.val != prev.val) {
                    last.next = curr;
                    last = curr;
                }
                curr = curr.next;
            }
        }
        last.next = null;
        return sentinel.next;
    }
}
